package com.eightbears.beboy.adapter;

import android.text.Html;
import android.text.Spanned;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eightbears.beboy.R;
import com.eightbears.beboy.base.util.CommonAPI;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.entity.ProductsEntity;
import com.eightbears.beboy.entity.UserEntity;

/**
 * Created by Bao on 2016/12/7.
 */

public class BuyVIPListAdapter extends BaseQuickAdapter<ProductsEntity, BaseViewHolder> {

    public BuyVIPListAdapter() {
        super(R.layout.item_vip, null);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, ProductsEntity productsEntity) {

        UserEntity userEntity = null;
        TextView byBtn = baseViewHolder.getView(R.id.tv_by_gold);

        if(null == userEntity){
            userEntity = SPUtil.getUser();
        }

        String title = productsEntity.getTitle();
        String price = productsEntity.getMoney();
        Spanned content = Html.fromHtml(productsEntity.getContent().getContent());
        String buttonText = "";
        String year;
        if (title.equals(CommonAPI.PARAMS_VIP_LEVE_1)){
            year = "半年";
            Glide.with(mContext).load(R.mipmap.icon_vip1).crossFade().error(R.mipmap.default_head).into((ImageView) baseViewHolder.getView(R.id.iv_vip));
        } else {
            year = "一年";
            Glide.with(mContext).load(R.mipmap.icon_vip2).crossFade().error(R.mipmap.default_head).into((ImageView) baseViewHolder.getView(R.id.iv_vip));
        }

        if(userEntity.getData().getVip_level() > 0){
            buttonText = "已开通";
            byBtn.setEnabled(false);
            byBtn.setBackgroundResource(R.drawable.bg_gray_style);
            byBtn.setCompoundDrawables(null, null, null, null);
        } else {
            buttonText = "开通";
            byBtn.setEnabled(true);
            byBtn.setBackgroundResource(R.drawable.background_circle_bead_blue_style);
        }

        baseViewHolder.setText(R.id.tv_title, productsEntity.getTitle())
                .setText(R.id.tv_price, price + "元/" + year)
                .setText(R.id.tv_content, content)
                .setText(R.id.tv_by_gold, buttonText)
                .addOnClickListener(R.id.tv_by_gold);
    }
}
